<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
function sethighlight($string) {
	$colorarray = array('', '#EE1B2E', '#EE5023', '#996600', '#3C9D40', '#2897C5', '#2B65B7', '#8F2A90', '#EC1282');
	$string = sprintf('%02d', $string);
	$stylestr = sprintf('%03b', $string[0]);
	$highlight = ' style="';
	$highlight .= $stylestr[0] ? 'font-weight: bold;' : '';
	$highlight .= $stylestr[1] ? 'font-style: italic;' : '';
	$highlight .= $stylestr[2] ? 'text-decoration: underline;' : '';
	$highlight .= $string[1] ? 'color: '.$colorarray[$string[1]].';' : '';
	$highlight .= '"';
	return $highlight;
}
function deal_with_threads($threads,$type) {
	global $_G; 
	$result = array();
	$study_nge = $_G['cache']['plugin']['study_nge'];
	$result[fid] = $threads['fid'];
	$result[tid] = $threads['tid'];
	$result['subject'] = cutstr($threads['subject'], $study_nge['subject_length'], '');
	$result['subject_title'] = strip_tags($threads['subject']);
	$result['dateline'] = gmdate('Y-m-d H:i', $threads['dateline'] + $_G['setting']['timeoffset'] * 3600);
	$result['lastpost'] = gmdate('Y-m-d H:i', $threads['lastpost'] + ($_G['setting']['timeoffset'] * 3600));
	$result['authorid'] = $threads['authorid'];
	$result['author'] = lang('plugin/study_nge', 'study_nge_func_left').strip_tags($threads['author']).lang('plugin/study_nge', 'study_nge_func_right');
	$result['views'] = $threads['views'];
	$result['replies'] = $threads['replies'];
	if($result['replies'] != '0' && !empty($result['replies'])){
		$result['lastposters'] = strip_tags($threads['lastposter']);
		$forum_post_result_first = forum_post_result_first_message_by_tid($threads[tid]);
		$result['lastreply'] = study_messagecutstr($forum_post_result_first[message],'50');
		$result['lastreply_uid'] = $forum_post_result_first[authorid];
		if(empty($result['lastreply'])){
			$result['lastreply'] = lang('plugin/study_nge', 'study_nge_func_lastreply_1');
		}
		$result['lastreply'] = lang('plugin/study_nge', 'study_nge_func_left').$result['lastposters'].lang('plugin/study_nge', 'study_nge_func_right').lang('plugin/study_nge', 'study_nge_func_colon').$result['lastreply'];
	}else{
		$result['lastreply'] = lang('plugin/study_nge', 'study_nge_func_lastreply_2');
	}
	
	if($study_nge[common_tiezi_right] == '3'){
			$target_blank_radio = $study_nge['target_blank_radio'] == 1 ? ' target="_blank"' : '';
			if($type != 'newpost'){
					$result['t_right_common'] = $result['lastposters'] ? $result['lastposters'] : $threads['author'];
					$result['t_right_common'] = '<a href="home.php?mod=space&uid='.($result['lastreply_uid'] ? $result['lastreply_uid'] : $result['authorid']).'" title="'.$result['t_right_common'].'" style="color:#95B9FF"'.$target_blank_radio.' >['.$result['t_right_common'].']</a>';
			}else{
					$result['t_right_other'] = $result['t_right_common'] = '<a href="home.php?mod=space&uid='.$result['authorid'].'" title="'.$threads['author'].'" style="color:#95B9FF"'.$target_blank_radio.' >['.$threads['author'].']</a>';
					
			}
	}elseif($study_nge[common_tiezi_right] == '2'){
			if($type != 'newpost'){
					$result['t_right_common'] = lang('plugin/study_nge', 'study_nge_date').$result['lastpost'];
			}else{
					$result['t_right_other'] = lang('plugin/study_nge', 'study_nge_date').$result['dateline'];
			}
	}
	
	$result['highlight'] = ($threads['highlight'] && $study_nge['highlight_radio']) ? sethighlight($threads['highlight']) : '';
	return $result;
}

function list_array($fids_show) {
	global $_G,$study_nge;
	$i = '1314';
	
	foreach ($fids_show as $id=> $fid){
		if(!empty($fid) && $fid){
			if($i == '1314'){
				$result .= $fid;
				$i = '0';
			}else{
				$result .= ','.$fid;
			}
		}
	}
	return $result;
}

function study_messagecutstr($str, $length = 0, $dot = ' ...') {
	global $_G;
	$sppos = strpos($str, chr(0).chr(0).chr(0));
	if($sppos !== false) {
		$str = substr($str, 0, $sppos);
	}
	$language = lang('forum/misc');
	loadcache(array('bbcodes_display', 'bbcodes', 'smileycodes', 'smilies', 'smileytypes', 'domainwhitelist'));
	$bbcodes = 'b|i|u|p|color|size|font|align|list|indent|float';
	$bbcodesclear = 'email|code|free|table|tr|td|img|swf|flash|attach|media|audio|payto'.($_G['cache']['bbcodes_display'][$_G['groupid']] ? '|'.implode('|', array_keys($_G['cache']['bbcodes_display'][$_G['groupid']])) : '');
	$str = strip_tags(preg_replace(array(
			"/\[hide=?\d*\](.*?)\[\/hide\]/is",
			"/\[quote](.*?)\[\/quote]/si",
			$language['post_edit_regexp'],
			"/\[url=?.*?\](.+?)\[\/url\]/si",
			"/\[($bbcodesclear)=?.*?\].+?\[\/\\1\]/si",
			"/\[($bbcodes)=?.*?\]/i",
			"/\[\/($bbcodes)\]/i",
		), array(
			"[b]$language[post_hidden][/b]",
			'',
			'',
			'\\1',
			'',
			'',
			'',
		), $str));
	if($length) {
		$str = cutstr($str, $length, $dot);
	}
	$str = preg_replace($_G['cache']['smilies']['searcharray'], '', $str);
	return trim($str);
}
?>
